package com.cmcc.aregister;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import cn.jpush.android.api.JPushInterface;

import com.cmcc.aregister.cs.asntask.InterfaceUtils.Ad_bar;
import com.cmcc.aregister.cs.asntask.MyAsynTaskOther;
import com.cmcc.aregister.cs.cache.AdCache;
import com.cmcc.aregister.cs.constant.Arguments;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import com.cmcc.aregister.cs.constant.GlobalSetting;
import com.cmcc.aregister.cs.database.DatabaseAdapter;
import com.cmcc.aregister.cs.http.HttpAsyncTask;
import com.cmcc.aregister.cs.model.AdbarResultModel;
import com.cmcc.aregister.cs.model.NoticeModel;
import com.cmcc.aregister.cs.model.User;
import com.cmcc.aregister.cs.service.HaoyuanTipService;
import com.cmcc.aregister.cs.util.MyLogger;
import com.cmcc.aregister.cs.util.PollingUtils;
import com.cmcc.aregister.cs.util.UniqueKey;
import com.cmcc.aregister.cs.util.Utils;
import com.cmcc.aregister.ui.activity.NewIndexActivity;
import com.cmcc.aregister.ui.activity.NoticeDetailActivity;
import com.cmcc.aregister.ui.activity.UserLoginActivity;
import com.cmcc.sdk.CmccDataStatistics;
import com.oauth.utils.InstalltionIdFactory;

/**
 * 加载页
 * 
 */
public class LoadingActivity extends Activity
{
    private static final String TAG = "LoadingActivity";
    public static final MyLogger logger = MyLogger.getLogger();
    protected Controller mHttpManager = null;
    /** 通知管理对象 */
    private NotificationManager mNotificationManager;
    /** 通知栏通知 */
    private Notification mNotification;
    DatabaseAdapter mDatabaseAdapter;
    private String metricsStr = "";
    private String areaId = "";
    private int width = 0;
    private int height = 0;

    private boolean needChange()
    {
        mDatabaseAdapter.open_user();
        Cursor c = mDatabaseAdapter.fetchAllUserData();
        if (c != null && c.getCount() > 0)
        {
            c.moveToFirst();
            try
            {
                if (!c.getString(c.getColumnIndex("mobile")).toString()
                    .equals(""))
                {
                    UniqueKey
                        .decodeStr(c.getString(c.getColumnIndex("mobile")));
                }
            }
            catch (Exception e)
            {
                // TODO: handle exception
                return true;
            }
        }
        c.close();
        mDatabaseAdapter.close_user();
        return false;
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this); // 初始化 JPush
        // 注册别名
        String alias = InstalltionIdFactory.id(this).replaceAll("-", "");
        JPushInterface.setAlias(this, alias, null);
        Log.i(TAG, "别名："+alias);
        // 程序启动时，号源提醒服务开启
        PollingUtils.startPollingService(this, 60, HaoyuanTipService.class,
            HaoyuanTipService.ACTION);
        mHttpManager = Controller.getInstance();
        mHttpManager.setCxt(this);
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mDatabaseAdapter = new DatabaseAdapter(this);
        if (needChange())
        {// 如果是以前未加密的版本升级到已经加密的数据版本
            mDatabaseAdapter.deleteAllUserData();
            ConfigUtilHelper.removeData(this);
        }
        DisplayMetrics dm = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;
        height = dm.heightPixels;
        if (ConfigUtilHelper.debug)
        {
            Log.i(TAG, "手机屏幕分辨率：" + width + "*" + height);
        }
        // metricsStr = width + "*" + height;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.p_loading);
        // getNotice();
        begin();

        if (ConfigUtilHelper.isLogin(this))
        {
            CmccDataStatistics.Comm_Login(this,
                ConfigUtilHelper.getMobile(this));
        }

    }

    private void getNotice()
    {
        getNoticeTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_GET_NOTICE, Arguments.GET_NOTICE_URL,
            HttpAsyncTask.CACHE_TYPE_ACTION, false);
        getNoticeTask.setUIHandler(getNoticeTaskHandler);
        mHttpManager.execute(getNoticeTask);
    }

    private List<NoticeModel> notices;
    private HttpAsyncTask getNoticeTask;
    Handler getNoticeTaskHandler = new Handler() {
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
            case HttpAsyncTask.TASK_NOTIFY_RETURN_DATA:
                if (msg.obj != null)
                {
                    Map<String, Object> map = (Map<String, Object>) msg.obj;
                    notices = (ArrayList<NoticeModel>) map
                        .get(Arguments.XML_NOTICE_LIST);
                    NoticeModel notice = getTopNotice(notices);
                    if (notice != null)
                    {
                        createNotify(notice);
                    }
                }
                break;
            }
        };
    };

    /**
     * 将日期转换为time
     * @param date 格式：yyyy-MM-dd HH:mm:ss
     * @return
     */
    private long date2time(String date)
    {
        long dtime = 0L;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
            Locale.CHINA);
        try
        {
            Date d = sdf.parse(date);
            dtime = d.getTime();
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return dtime;
    }

    /**
     * 获取首页公告
     * @param list
     * @return
     */
    private NoticeModel getTopNotice(List<NoticeModel> list)
    {
        NoticeModel notice = null;
        long topTime = 0;
        if (list != null && list.size() > 0)
        {
            for (NoticeModel noticeModel : list)
            {
                if (noticeModel.top)
                {
                    long tmp = date2time(noticeModel.create_time);
                    if (tmp > topTime)
                    {
                        topTime = tmp;
                        notice = noticeModel;
                    }
                }
                else
                {
                    continue;
                }
            }
        }
        return notice;
    }

    private void createNotify(NoticeModel notice)
    {
        int id = ConfigUtilHelper.getLastNoticeId(this);
        if (notice.id == id)
        {
            return;
        }
        else
        {
            ConfigUtilHelper.saveLastNoticeId(this, notice.id);
        }
        // 创建通知对象
        mNotification = new Notification(R.drawable.icon, "深圳预约挂号公告",
            System.currentTimeMillis());
        // 添加声音
        mNotification.defaults |= Notification.DEFAULT_SOUND;
        mNotification.flags = Notification.FLAG_AUTO_CANCEL;
        Intent intent = new Intent(this, NoticeDetailActivity.class);
        intent.putExtra(Arguments.NOTICE_MODEL, notice);
        intent.putExtra(Arguments.NOTICE_JUMP_TYPE,
            Arguments.NOTICE_JUMP_TYPE_NOTIFICATION);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, 0);
        mNotification.setLatestEventInfo(this, "深圳预约挂号", notice.title,
            contentIntent);
        mNotificationManager.notify(1, mNotification);
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        JPushInterface.onPause(this);
        CmccDataStatistics.onStop(this);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        JPushInterface.onResume(this);
        if (!autoLogin)
        {
            // startIndexActivity();
            loadAdStartActivity();
            return;
        }
        CmccDataStatistics.onStart(this);
    }

    boolean autoLogin = true;

    /**
     * 睡眠一秒后进入首页
     */
    private void begin()
    {
        AsyncTask<String, Integer, String> task = new AsyncTask<String, Integer, String>() {
            protected String doInBackground(String... params)
            {
                try
                {
                    Thread.sleep(2000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                return null;
            }

            protected void onPostExecute(String result)
            {
                /*
                 * if(ConfigUtilHelper.getAutoLogin(LoadingActivity.this)){
                 * autoLogin = false; initLogin(); }else{ startIndexActivity();
                 * }
                 */
                // startIndexActivity();
                loadAdStartActivity();
            }
        };
        task.execute("");
    }

    private void initLogin()
    {
        Intent intent = new Intent(LoadingActivity.this,
            UserLoginActivity.class);
        intent.putExtra(Arguments.AUTO_LOGIN, true);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    // 启动页广告。
    Ad_bar startAd_bar = new Ad_bar() {
        @Override
        public void ad_bar_callback(AdbarResultModel model)
        {
            startIndexActivity(model);
        }

    };

    /**
     * 加载启动页广告
     */
    private void loadAdStartActivity()
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap = initArgMap(parameterMap);

        metricsStr = adapterMetrics();
        if (metricsStr.equals(Arguments.mobileMetrics1280720))
        {
            areaId = Arguments.StartAreaId1280720;
        }
        else if (metricsStr.equals(Arguments.mobileMetrics19201080))
        {
            areaId = Arguments.StartAreaId19201080;
        }
        else
        {
            areaId = Arguments.StartAreaIdOther;
        }
        parameterMap.put("areaId", areaId);
        AdCache.areaId = areaId;
        parameterMap.put("methodVit", Arguments.GetAdBar);
        new MyAsynTaskOther(startAd_bar, GlobalSetting.AD_BAR, parameterMap)
            .execute();

    }

    /**
     * 分辨率 720*1280 1080*1920 两种在100值上下适配
     * @return
     */
    private String adapterMetrics()
    {
        int trueWidth = 0;
        int trueHight = 0;
        int addWidth = width + 100;
        int delWidth = width - 100;
        int addHeight = height + 100;
        int delHeight = height - 100;

        if (addWidth >= 720 && delWidth <= 720)
        {
            trueWidth = 720;
            if (addHeight >= 1280 && delHeight <= 1280)
            {
                trueHight = 1280;
            }
        }
        else if (addWidth >= 1080 && delWidth <= 1080)
        {
            trueWidth = 1080;
            if (addHeight >= 1920 && delHeight <= 1920)
            {
                trueHight = 1920;
            }
        }
        return trueHight + "*" + trueWidth;
    }

    /**
     * 初始化参数
     * 
     * @param parameterMap
     * @return
     */
    private Map<String, String> initArgMap(Map<String, String> parameterMap)
    {
        parameterMap.put("userId", Arguments.UserId_AD);
        parameterMap.put("timestamp", ConfigUtilHelper.getStringNow());
        parameterMap.put("userPwd", ConfigUtilHelper.getAdUserPwd());
        return parameterMap;
    }

    /**
     * 进入首页并关闭加载页
     */
    public void startIndexActivity(AdbarResultModel model)
    {
        Intent intent = new Intent(this, NewIndexActivity.class);
        if (model.ads.size() > 0)
        {
            AdCache.imgUrl = model.ads.get(0).pic;// 广告图片地址
            AdCache.hasUrl = model.ads.get(0).hasAdUrl;// 是否有广告访问地址1 有
            AdCache.playtime = model.ads.get(0).playtime;// 广告播放时间
            // 2 没有
            AdCache.adId = model.ads.get(0).id;// 广告ID
            AdCache.scantype = model.ads.get(0).scantype;// 浏览方式 1 浏览器
            // 2客户端
        }
        intent.putExtra("isStart", true);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    // 深圳移动无线城市-医疗频道手机客户端接口文档(3).doc中21个请求的测试使用
    public void httpTest()
    {
        User user = new User();
        user.mobile = "13761874920";
        user.name = "张三";
        user.birth = "1988-03-12";
        user.sex = "0";
        user.card = "223333333333333333";
        user.social_card = "";
        sendUserRegisterTask(user);
        sendChangeUserInfoTask(user);
        sendGetUserInfoTask(user.mobile);
        sendGetOrderInfoTask(user.mobile);
        // sendGetHospitalListTask("2", user.mobile, "0", "0", "10");
        // sendGetHospitalDetailTask(user.mobile, "0", "2");
        // sendGetDepartmentListTask("2", user.mobile, "", "", "1", "10");
        // sendGetDepartmentDetailTask(user.mobile, "1", "1");
        // sendGetDoctorListTask("2", user.mobile, "", "", "", "1", "10");
        // sendGetDoctorDetailTask(user.mobile, "1", "1", "1");
        // sendSearchHospitalTask("0", "7", "", "深圳", "1", "10");
        // sendSearchDepartmentTask("0", "A", "科", "1", "10");
        // sendSearchDoctorTask("0", "张", "1", "10");
        // sendGetScheduleListTask("22", "361251");
        // sendConfirmAppointmentTask(user.mobile, "22", "993804", "", "1");
        sendCancelAppointmentTask(user.mobile, "22", "");
        // sendConfirmAttentionTask(user.mobile, "0", "0", "1");
        // sendCancelAttentionTask(user.mobile, "0", "0", "1");
        sendGetDiseaseListTask("", "1", "10");
        sendGetDiseaseDetailTask("1");
        sendGetOtherOrderTask("13480664827");
        // sendOtherOrderTask("13480664827", "复旦大学附属华山医院%124普外科%124乐竹琴test",
        // "972", "上午10点-11点");
        sendCancelOtherOrder("1");
        // ---------------
        sendGetDoctorListTask("0", user.mobile, "", "", "1", "0", "");
    }

    /**
     * 用户注册
     */
    private HttpAsyncTask sendUserRegisterTask;

    private void sendUserRegisterTask(User user)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("mobile", user.mobile);
        parameterMap.put("name", user.name);
        parameterMap.put("birth", user.birth);
        parameterMap.put("sex", user.sex);
        parameterMap.put("card", user.card);
        parameterMap.put("social_card", user.social_card);
        String userRegisterUrl = Utils.changeUrl(
            Arguments.SERVER_URL_USER_REGISTER, parameterMap);
        // String userRegisterUrl =
        // "http://211.139.161.120/wxtest/api2/index.php?c=sch&a=addMember&client_auth=test&token=123456&type=0&&name=刈少&mobile=13524676784&birth=1987-5-6&sex=0&card=46895433678&social_card=";
        sendUserRegisterTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_USER_REGISTER, userRegisterUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendUserRegisterTask);
    }

    /**
     * 修改用户信息
     */
    private HttpAsyncTask sendChangeUserInfoTask;

    private void sendChangeUserInfoTask(User user)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("mobile", user.mobile);
        parameterMap.put("name", user.name);
        parameterMap.put("birth", user.birth);
        parameterMap.put("sex", user.sex);
        parameterMap.put("card", user.card);
        parameterMap.put("social_card", user.social_card);
        String changeUserInfoUrl = Utils.changeUrl(
            Arguments.SERVER_URL_CHANGE_USER_INFO, parameterMap);
        sendChangeUserInfoTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_CHANGE_USER_INFO, changeUserInfoUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendChangeUserInfoTask);
    }

    /**
     * 获取用户信息
     */
    private HttpAsyncTask sendGetUserInfoTask;

    private void sendGetUserInfoTask(String mobile)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("mobile", mobile);
        String changeUserInfoUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_USER_INFO, parameterMap);
        sendGetUserInfoTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_GET_USER_INFO, changeUserInfoUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendGetUserInfoTask);
    }

    /**
     * 获取用户订单信息
     */
    private HttpAsyncTask sendGetOrderInfoTask;

    private void sendGetOrderInfoTask(String mobile)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("mobile", mobile);
        String changeUserInfoUrl = Utils.changeUrl(
            Arguments.SERVER_URL_CHECK_ORDER_INFO, parameterMap);
        sendGetOrderInfoTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_GET_ORDER_INFO, changeUserInfoUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendGetOrderInfoTask);
    }

    /**
     * 获取医院列表
     */
    private HttpAsyncTask sendGetHospitalListTask;

    /**
     * 
     * @param area_type 区域类别 0：本地 1：异地 2 所有
     * @param mobile 手机号码 如果为空，则不处理关注相关数据
     * @param is_attention 是否已关注 缺省时查询全部，1只返回已关注数据
     * @param page 页数 0 表示不分页为所有，分页从1开始
     * @param pageSize 每页记录数 默认为10
     */
    protected void sendGetHospitalListTask(String area_type, String mobile,
        String is_attention, String page, String pageSize)
    {
        logger.d("----------sendGetHospitalListTask----------");
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("area_type", area_type);
        parameterMap.put("mobile", mobile);
        parameterMap.put("is_attention", is_attention);
        parameterMap.put("page", page);
        parameterMap.put("pageSize", pageSize);
        String getHospitalListUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_HOSPITAL_LIST, parameterMap);
        sendGetHospitalListTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_HOSPITAL_LIST, getHospitalListUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        sendGetHospitalListTask.setUIHandler(sendGetHospitalListHandler);
        Controller.getInstance().execute(sendGetHospitalListTask);
    }

    Handler sendGetHospitalListHandler = new Handler() {
        public void handleMessage(Message msg)
        {
            logger.d("----------sendGetHospitalListHandler----------");
            switch (msg.what)
            {
            case HttpAsyncTask.TASK_NOTIFY_RETURN_DATA:
                break;
            case HttpAsyncTask.TASK_NOTIFY_CONNECTION_FAIL:
                break;
            }
        }
    };

    /**
     * 获取医院信息
     */
    private HttpAsyncTask sendGetHospitalDetailTask;

    /**
     * 
     * @param mobile 手机号码 如果为空，则不处理关注相关数据
     * @param area_type 区域类别 0：本地 1：异地
     * @param unit_id 医院编号
     */
    private void sendGetHospitalDetailTask(String mobile, String area_type,
        String unit_id)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("area_type", area_type);
        parameterMap.put("mobile", mobile);
        parameterMap.put("unit_id", unit_id);
        String getHospitalDetailUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_HOSPITAL_DETAIL, parameterMap);
        sendGetHospitalDetailTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_HOSPITAL_DETAIL, getHospitalDetailUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendGetHospitalDetailTask);
    }

    /**
     * 获取科室列表
     */
    private HttpAsyncTask sendGetDepartmentListTask;

    /**
     * 
     * @param area_type 区域类别 0：本地 1：异地 2 所有
     * @param mobile 手机号码 如果为空，则不处理关注相关数据
     * @param unit_id 医院id 如果为空，获取所有的科室， 如果不为空，获取所在医院的科室
     * @param is_attention 是否已关注 缺省时查询全部，1只返回已关注数据
     * @param page 页数 0 表示不分页为所有，分页从1开始
     * @param pageSize 每页记录数 默认为10
     */
    protected void sendGetDepartmentListTask(String area_type, String mobile,
        String unit_id, String is_attention, String page, String pageSize)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("area_type", area_type);
        parameterMap.put("mobile", mobile);
        parameterMap.put("unit_id", unit_id);
        parameterMap.put("is_attention", is_attention);
        parameterMap.put("page", page);
        parameterMap.put("pageSize", pageSize);
        String getDepartmentListUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_DEPARTMENT_LIST, parameterMap);
        sendGetDepartmentListTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_DEPARTMENT_LIST, getDepartmentListUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendGetDepartmentListTask);
    }

    /**
     * 获取科室信息
     */
    private HttpAsyncTask sendGetDepartmentDetailTask;

    /**
     * 
     * @param mobile 手机号码 如果为空，则不处理关注相关数据
     * @param area_type 区域类别 0：本地 1：异地
     * @param dep_id 科室id
     */
    private void sendGetDepartmentDetailTask(String mobile, String area_type,
        String dep_id)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("area_type", area_type);
        parameterMap.put("mobile", mobile);
        parameterMap.put("dep_id", dep_id);
        String getDepartmentDetailUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_DEPARTMENT_DETAIL, parameterMap);
        sendGetDepartmentDetailTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_DEPARTMENT_DETAIL, getDepartmentDetailUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendGetDepartmentDetailTask);
    }

    /**
     * 获取医生列表
     */
    private HttpAsyncTask sendGetDoctorListTask;

    /**
     * 
     * @param area_type 区域类别 0：本地 1：异地 2 所有
     * @param mobile 手机号码 如果为空，则不处理关注相关数据
     * @param unit_id 医院id 如果为空，获取所有的科室， 如果不为空，获取所在医院的科室
     * @param dep_id 科室id 如果为空，获取所有的医生， 如果不为空，获取所在科室的医生
     * @param is_attention 是否已关注 缺省时查询全部，1只返回已关注数据
     * @param page 页数 0 表示不分页为所有，分页从1开始
     * @param pageSize 每页记录数 默认为10
     */
    protected void sendGetDoctorListTask(String area_type, String mobile,
        String unit_id, String dep_id, String is_attention, String page,
        String pageSize)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("area_type", area_type);
        parameterMap.put("mobile", mobile);
        parameterMap.put("unit_id", unit_id);
        parameterMap.put("dep_id", dep_id);
        parameterMap.put("is_attention", is_attention);
        parameterMap.put("page", page);
        parameterMap.put("pageSize", pageSize);
        String getDoctorListUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_DOCTOR_LIST, parameterMap);
        sendGetDoctorListTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_DOCTOR_LIST, getDoctorListUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendGetDoctorListTask);
    }

    /**
     * 获取医生信息
     */
    private HttpAsyncTask sendGetDoctorDetailTask;

    /**
     * 
     * @param mobile 手机号码 如果为空，则不处理关注相关数据
     * @param area_type 区域类别 0：本地 1：异地
     * @param dep_id 科室id
     * @param doctor_id 医生id
     */
    private void sendGetDoctorDetailTask(String mobile, String area_type,
        String dep_id, String doctor_id)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("area_type", area_type);
        parameterMap.put("mobile", mobile);
        parameterMap.put("dep_id", dep_id);
        parameterMap.put("doctor_id", doctor_id);
        String getDoctorDetailUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_DOCTOR_DETAIL, parameterMap);
        sendGetDoctorDetailTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_DOCTOR_DETAIL, getDoctorDetailUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendGetDoctorDetailTask);
    }

    /**
     * 查询名医名院
     */
    private HttpAsyncTask sendSearchTask;

    /**
     * @param area_type 区域类别 N 0：本地 1：异地
     * @param search_type 查询类别 N 0:搜索医院1:搜索科室2:搜索医生
     * @param area_id 医院区域 Y 本地: 7罗湖区，3317福田区，3318盐田区，3319龙岗区，8南山区，3320宝安区，3321
     *        光明新区； 异地： 1 广州，6北京，7上海，9武汉，5长沙，10成都，11西安，8重庆
     * @param unit_level 医院级别 Y
     * @param cat_no 科室分类 Y
     *        A内科；B外科；C肿瘤科；D妇产科；E儿科；F五官科；G男科；H传染科；I皮肤性病；J精神心理科K中西医结合科
     *        L生殖健康M急诊科N老年科
     * @param search_content 查询内容 Y
     * @param page 页数 N 0 表示不分页为所有，分页从1开始
     * @param pageSize 每页记录数 11 Y 默认为10
     */
    private void sendSearchTask(String area_type, String search_type,
        String area_id, String unit_level, String cat_no,
        String search_content, String page, String pageSize)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("area_type", area_type);
        parameterMap.put("search_type", search_type);
        parameterMap.put("area_id", area_id);
        parameterMap.put("unit_level", unit_level);
        parameterMap.put("cat_no", cat_no);
        parameterMap.put("search_content", search_content);
        parameterMap.put("page", page);
        parameterMap.put("pageSize", pageSize);
        String searchUrl = Utils.changeUrl(
            Arguments.SERVER_URL_SEARCH_FRMOUS_HOSPITAL, parameterMap);
        sendSearchTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_SEARCH, searchUrl, HttpAsyncTask.CACHE_TYPE_SAVE,
            false);
        Controller.getInstance().execute(sendSearchTask);
    }

    private void sendSearchHospitalTask(String area_type, String area_id,
        String unit_level, String search_content, String page, String pageSize)
    {
        sendSearchTask(area_type, "0", area_id, unit_level, "", search_content,
            page, pageSize);
    }

    private void sendSearchDepartmentTask(String area_type, String cat_no,
        String search_content, String page, String pageSize)
    {
        sendSearchTask(area_type, "1", "", "", cat_no, search_content, page,
            pageSize);
    }

    private void sendSearchDoctorTask(String area_type, String search_content,
        String page, String pageSize)
    {
        sendSearchTask(area_type, "2", "", "", "", search_content, page,
            pageSize);
    }

    /**
     * 查询出诊班次的号源信息
     */
    private HttpAsyncTask sendGetScheduleListTask;

    /**
     * 
     * @param unit_id 医院ID
     * @param schedule_id 班次
     */
    private void sendGetScheduleListTask(String unit_id, String schedule_id)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("unit_id", unit_id);
        parameterMap.put("schedule_id", schedule_id);
        String getScheduleListUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_SCHEDULE_LIST, parameterMap);
        sendGetScheduleListTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_SCHEDULE_LIST, getScheduleListUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendGetScheduleListTask);
    }

    /**
     * 提交本地预约订单
     */
    private HttpAsyncTask sendConfirmAppointmentTask;

    /**
     * 
     * @param mobile 手机号码
     * @param unit_id 医院ID
     * @param detl_id 号源编号
     * @param clinic_no 就诊卡号
     * @param pay_method 支付方式 1 小额支付；2 现场支付；4 手机支付
     */
    private void sendConfirmAppointmentTask(String mobile, String unit_id,
        String detl_id, String clinic_no, String pay_method)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("mobile", mobile);
        parameterMap.put("unit_id", unit_id);
        parameterMap.put("detl_id", detl_id);
        parameterMap.put("clinic_no", clinic_no);
        parameterMap.put("pay_method", pay_method);
        String confirmAppointmentUrl = Utils.changeUrl(
            Arguments.SERVER_URL_CONFIRM_APPOINTMENT, parameterMap);
        sendConfirmAppointmentTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_CONFIRM_APPOINTMENT, confirmAppointmentUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendConfirmAppointmentTask);
    }

    /**
     * 取消本地预约订单
     */
    private HttpAsyncTask sendCancelAppointmentTask;

    /**
     * 
     * @param mobile 手机号码
     * @param unit_id 医院ID
     * @param yuyue_id 订单ID
     */
    private void sendCancelAppointmentTask(String mobile, String unit_id,
        String yuyue_id)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("mobile", mobile);
        parameterMap.put("unit_id", unit_id);
        parameterMap.put("yuyue_id", yuyue_id);
        String cancelAppointmentUrl = Utils.changeUrl(
            Arguments.SERVER_URL_CANCEL_APPOINTMENT, parameterMap);
        sendCancelAppointmentTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_CANCEL_APPOINTMENT, cancelAppointmentUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendCancelAppointmentTask);
    }

    /**
     * 我要关注
     */
    private HttpAsyncTask sendConfirmAttentionTask;

    /**
     * 
     * @param mobile 手机号码
     * @param attentionType 关注类别 N 0：关注医院1:关注科室2:关注医生
     * @param area_type 医院区域 N 0：本地 1是异地
     * @param id 相应id
     */
    private void sendConfirmAttentionTask(String mobile, String attentionType,
        String area_type, String id)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("mobile", mobile);
        parameterMap.put("area_type", area_type);
        if ("0".equals(attentionType))
        {
            parameterMap.put("unit_id", id);
        }
        else if ("1".equals(attentionType))
        {
            parameterMap.put("dep_id", id);
        }
        else
        {
            parameterMap.put("doctor_id", id);
        }
        String confirmAttentionUrl = Utils.changeUrl(
            Arguments.SERVER_URL_CONFIRM_ATTENTION, parameterMap);
        sendConfirmAttentionTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_CONFIRM_ATTENTION, confirmAttentionUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendConfirmAttentionTask);
    }

    /**
     * 取消关注
     */
    private HttpAsyncTask sendCancelAttentionTask;

    /**
     * 
     * @param mobile 手机号码
     * @param attentionType 关注类别 N 0：关注医院1:关注科室2:关注医生
     * @param area_type area_type 区域ID Y 0是本地 1是异地 2是全部
     * @param id 相应id
     */
    private void sendCancelAttentionTask(String mobile, String attentionType,
        String area_type, String id)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("mobile", mobile);
        parameterMap.put("area_type", area_type);
        if ("0".equals(attentionType))
        {
            parameterMap.put("unit_id", id);
        }
        else if ("1".equals(attentionType))
        {
            parameterMap.put("dep_id", id);
        }
        else
        {
            parameterMap.put("doctor_id", id);
        }
        String cancelAttentionUrl = Utils.changeUrl(
            Arguments.SERVER_URL_CANCEL_ATTENTION, parameterMap);
        sendCancelAttentionTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_CANCEL_ATTENTION, cancelAttentionUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendCancelAttentionTask);
    }

    /**
     * 获取疾病列表
     */
    private HttpAsyncTask sendGetDiseaseListTask;

    /**
     * 
     * @param search_content 查询内容 Y 为空时查询所有
     * @param page 页数 N 0 表示不分页为所有，分页从1开始
     * @param pageSize 每页记录数 11 Y 默认为10
     */
    private void sendGetDiseaseListTask(String search_content, String page,
        String pageSize)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("search_content", search_content);
        parameterMap.put("page", page);
        parameterMap.put("pageSize", pageSize);
        String getDiseaseListUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_DISEASE_LIST, parameterMap);
        sendGetDiseaseListTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_GET_DISEASE_LIST, getDiseaseListUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendGetDiseaseListTask);
    }

    /**
     * 获取疾病信息
     */
    private HttpAsyncTask sendGetDiseaseDetailTask;

    /**
     * 
     * @param ill_id 疾病ID
     */
    private void sendGetDiseaseDetailTask(String ill_id)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("ill_id", ill_id);
        String getDiseaseDetailUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_DISEASE_DETAIL, parameterMap);
        sendGetDiseaseListTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_GET_DISEASE_DETAIL, getDiseaseDetailUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendGetDiseaseListTask);
    }

    /**
     * 异地就医订单查询
     */
    private HttpAsyncTask sendGetOtherOrderTask;

    /**
     * 
     * @param mobile 手机号码
     */
    private void sendGetOtherOrderTask(String mobile)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("mobile", mobile);
        String getOtherOrderUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_OTHER_ORDER, parameterMap);
        sendGetOtherOrderTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_GET_OTHER_ORDER, getOtherOrderUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendGetOtherOrderTask);
    }

    /**
     * 异地就医下单
     */
    private HttpAsyncTask sendOtherOrderTask;

    /**
     * 
     * @param mobile
     * @param service_desc 用户需求描述
     * @param unit_id 医院ID
     * @param link_best_time 最佳联系时间 现在;30分钟后; 1小时后; 上午10点-11点; 中午12点-13点;
     *        晚上19点-20点
     */
    private void sendOtherOrderTask(String mobile, String service_desc,
        String unit_id, String link_best_time)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("mobile", mobile);
        parameterMap.put("service_desc", service_desc);
        parameterMap.put("unit_id", unit_id);
        parameterMap.put("link_best_time", link_best_time);
        String orderOtheUrl = Utils.changeUrl(Arguments.SERVER_URL_ORDER_OTHE,
            parameterMap);
        sendOtherOrderTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_ORDER_OTHE, orderOtheUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendOtherOrderTask);
    }

    /**
     * 异地就医订单取消
     */
    private HttpAsyncTask sendCancelOtherOrder;

    /**
     * 
     * @param order_no 订单编号
     */
    private void sendCancelOtherOrder(String order_no)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("order_no", order_no);
        String cancelOtherOrderUrl = Utils.changeUrl(
            Arguments.SERVER_URL_CANCEL_OTHER_ORDER, parameterMap);
        sendCancelOtherOrder = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_CANCEL_OTHER_ORDER, cancelOtherOrderUrl,
            HttpAsyncTask.CACHE_TYPE_SAVE, false);
        Controller.getInstance().execute(sendCancelOtherOrder);
    }

}